package com.xsq.util;

import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.codehaus.xfire.util.Base64;

import com.alibaba.fastjson.JSON;
import com.common.utils.LogUtils;

public class RequestUtil {
	

	public static final String appkey = "20171207142349041001";
	public static final String secret_key = "02ab14eaf9f3f4ce4d3cac04be37b922";
	public static final String sign_method = "md5";
	public static final String v = "1.0";
	
	
	
	/**
	 * 上位机测试是否可用
	 * @param encrypt
	 * @param posno
	 * @param winno
	 * @return
	 */
	public static String generateServerTestRequest(boolean encrypt, String posno, String winno) {

		try {

			String request_xml = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno></posInfo>".replace("${POSNO}", posno).replace("${WINNO}", winno);

			TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
			apiparamsMap.put("format", "json");
			apiparamsMap.put("method", "validServer");
			apiparamsMap.put("sign_method", "md5");
			apiparamsMap.put("appKey", appkey);
			apiparamsMap.put("v", v);
			apiparamsMap.put("locale", "zh_CN");
			if (encrypt) {
				String base64_request_xml = Base64.encode(request_xml.getBytes("UTF-8"));
				apiparamsMap.put("request_xml", base64_request_xml);
				apiparamsMap.put("encrypt_method", "base64");
				// 生成签名
				String sign = Util.md5Signature(apiparamsMap, secret_key);
				apiparamsMap.put("sign", sign);
				apiparamsMap.put("request_xml", URLEncoder.encode(base64_request_xml, "UTF-8"));
			} else {
				apiparamsMap.put("request_xml", request_xml);
				// 生成签名
				String sign = Util.md5Signature(apiparamsMap, secret_key);
				apiparamsMap.put("sign", sign);
			}

			StringBuilder param = new StringBuilder();
			for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, String> e = it.next();
				param.append("&").append(e.getKey()).append("=").append(e.getValue());
			}

			LogUtils.info("parms:" + param.toString().substring(1));
			return param.toString().substring(1);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	//刷卡查询用户信息
	public static String generateSrchCardInfoRequest(boolean encrypt, String cardsno, String cardno) {

		String pos_request = "<userInfo><cardsno>${CARDSNO}</cardsno><cardno>${CARDNO}</cardno></userInfo>";
		String request_xml = pos_request.replace("${CARDSNO}", cardsno).replace("${CARDNO}", cardno);

		return generateRequestXML(encrypt,"queryCardInfo", request_xml);
	}
	
	//在线交易请求
	public static String generateOnlineConsumeRequest(boolean encrypt,String flowid, String logicno,
			String cardsno, String cardno,String amount,String  consumetime,String consumepwd) {

		String pos_request = "<PosConsume><flowid>${FLOWID}</flowid><cardsno>${CARDSNO}</cardsno>" +
				"<cardno>${CARDNO}</cardno><amount>${AMOUNT}</amount>" +
				"<logicno>${LOGICNO}</logicno><consumetime>${CONSUMETIME}</consumetime>" +
				"<consumepwd>${CONSUMEPWD}</consumepwd></PosConsume>";
	

		String request_xml = pos_request.replace("${FLOWID}", flowid).replace("${CARDSNO}", cardsno)
				.replace("${CARDNO}", cardno).replace("${AMOUNT}", amount)
				.replace("${LOGICNO}", logicno).replace("${CONSUMETIME}", consumetime).replace("${CONSUMEPWD}", "<![CDATA["+consumepwd+"]]>");
		
		return generateRequestXML(encrypt,"PosOnlineConsume", request_xml);
	}
	
	
	
	public static String generatePosAuthRequest(boolean encrypt, String possno, String posno, String winno) {

		String pos_request = "<posInfo><possno>${POSSNO}</possno><posno>${POSNO}</posno><winno>${WINNO}</winno></posInfo>";
	

		String request_xml = pos_request.replace("${POSSNO}", possno).replace("${POSNO}", posno).replace("${WINNO}", winno);

		
		return generateRequestXML(encrypt,"authBaseConfig", request_xml);
	}
	
	
	public static String generatePosLinkRequest(boolean encrypt,String posno,String curTime,Map<String,Object> otherInfo) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><curTime>${CURTIME}</curTime><otherInfo>${OTHERINFO}</otherInfo></posInfo>";
		String request_xml = pos_request.replace("${POSNO}", posno).replace("${CURTIME}", curTime).replace("${OTHERINFO}", JSON.toJSONString(otherInfo));
		return generateRequestXML(encrypt,"posRefreshLink", request_xml);
	}
	
	
	
	public static String generatePosFlowSrchRequest(boolean encrypt, String posno, String winno,String beginTime,String endTime,String page,String consumeType) {
		String pos_request = null;
		String request_xml = null;
		if(consumeType==null){
			pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><beginTime>${BEGINTIME}</beginTime><endTime>${ENDTIME}</endTime><page>${PAGE}</page></posInfo>";
			request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${BEGINTIME}", beginTime).replace("${ENDTIME}", endTime).replace("${PAGE}", page);
		}else{
			//加消费类型的
			pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><beginTime>${BEGINTIME}</beginTime><endTime>${ENDTIME}</endTime><page>${PAGE}</page><consumeType>${CONSUMETYPE}</consumeType></posInfo>";
			request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${BEGINTIME}", beginTime).replace("${ENDTIME}", endTime).replace("${PAGE}", page).replace("${CONSUMETYPE}", consumeType);
		}
		

		return generateRequestXML(encrypt,"srchpPosFlow", request_xml);
	}
	
	public static String generatePosFlowStatRequest(boolean encrypt, String posno, String winno,String beginTime,String endTime) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><beginTime>${BEGINTIME}</beginTime><endTime>${ENDTIME}</endTime></posInfo>";
	

		String request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${BEGINTIME}", beginTime).replace("${ENDTIME}", endTime);

		return generateRequestXML(encrypt,"srchFlowStat", request_xml);
	}
	
	public static String generateSrchJcLogRequest(boolean encrypt, String posno, String winno,String beginTime,String endTime,String page) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><beginTime>${BEGINTIME}</beginTime><endTime>${ENDTIME}</endTime><page>${PAGE}</page></posInfo>";
	

		String request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${BEGINTIME}", beginTime).replace("${ENDTIME}", endTime).replace("${PAGE}", page);

		return generateRequestXML(encrypt,"srchJcLog", request_xml);
	}
	
	
	
	public static String generatePosFlowJcRequest(boolean encrypt, String posno, String winno,String flowcode,String jymoney,String sjmoney) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><flowcode>${FLOWCODE}</flowcode><jyje>${JYMONEY}</jyje><sjje>${SJMONEY}</sjje></posInfo>";
	

		String request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${FLOWCODE}", flowcode).replace("${JYMONEY}", jymoney).replace("${SJMONEY}", sjmoney);

		return generateRequestXML(encrypt,"jcFlow", request_xml);
	}
		
	public static String generateSrchLateFlowRequest(boolean encrypt, String posno, String winno) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno></posInfo>";
	

		String request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno);

		return generateRequestXML(encrypt,"srchLateFlow", request_xml);
	}
	
	public static String generateConsumeBackRequest(boolean encrypt, String posno, String winno,String flowcode) {

		String pos_request = "<posInfo><posno>${POSNO}</posno><winno>${WINNO}</winno><flowcode>${FLOWCODE}</flowcode></posInfo>";
	

		String request_xml = pos_request.replace("${POSNO}", posno).replace("${WINNO}", winno).replace("${FLOWCODE}", flowcode);

		return generateRequestXML(encrypt,"doConsumeBack", request_xml);
	}
	
	
	
	/**
	 * authBaseConfig
	 * @param encrypt
	 * @param method
	 * @param request_xml
	 * @return
	 */
	public static String generateRequestXML(boolean encrypt,String method,String request_xml) {

		try {

			TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
			apiparamsMap.put("format", "json");
			apiparamsMap.put("method", method);
			apiparamsMap.put("sign_method", "md5");
			apiparamsMap.put("appKey", appkey);
			apiparamsMap.put("v", v);
			apiparamsMap.put("locale", "zh_CN");
			if (encrypt) {
				String base64_request_xml = Base64.encode(request_xml.getBytes("UTF-8"));
				apiparamsMap.put("request_xml", base64_request_xml);
				apiparamsMap.put("encrypt_method", "base64");
				// 生成签名
				String sign = Util.md5Signature(apiparamsMap, secret_key);
				apiparamsMap.put("sign", sign);
				apiparamsMap.put("request_xml", URLEncoder.encode(base64_request_xml, "UTF-8"));
			} else {
				apiparamsMap.put("request_xml", request_xml);
				// 生成签名
				String sign = Util.md5Signature(apiparamsMap, secret_key);
				apiparamsMap.put("sign", sign);
			}

			StringBuilder param = new StringBuilder();
			for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, String> e = it.next();
				param.append("&").append(e.getKey()).append("=").append(e.getValue());
			}

			//LogUtils.info("parms:" + param.toString().substring(1));
			return param.toString().substring(1);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
